<?php

namespace App\Logic\finger;

use App\Models\User;
use App\Logic\Code;
use Illuminate\Auth\Authenticatable;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\DB;
use Laravel\Lumen\Auth\Authorizable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Tymon\JWTAuth\Contracts\JWTSubject;

/**
 * @author AdamTyn
 * @description <用户>数据模型
 */
class UserLogic
{

    //注册
    public static function add($data,$scene){

        $validateRule = [
            'phone'                          => 'required',
            'password'                            => 'required',
            'code'                           =>'required',
            'inviter'                            => 'required',
        ];
        $errorMsg = [
            'phone.required'                 => '手机号不能为空',
            'password.required'                   => '密码不能为空',
            'code.required'                   => '验证码不能为空',
            'inviter.required'                   => '邀请码不能为空',
        ];

        $validator = Validator::make($data, $validateRule, $errorMsg);
        if ($validator->fails()){
            $errors = $validator->errors()->all();
            return e_msg(300001,$errors[0]);
        }

        $key = $data['phone'].'_code_'.$scene;

        $code_info = Code::code($key,$data['code']);
        if($code_info['status'] === 'ERROR'){
            return $code_info;
        }
        $phone_id = User::where('phone',$data['phone'])->value('id');
        if ( $phone_id ) {
            return e_msg(300004,'该手机号已注册');
        }
        return User::add($data);
    }


    public static function login($data){

        $validateRule = [
            'username'                          => 'required',
            'password'                            => 'required',
        ];
        $errorMsg = [
            'username.required'                 => '账号不能为空',
            'password.required'                   => '密码不能为空',
        ];

        $validator = Validator::make($data, $validateRule, $errorMsg);
        if ($validator->fails()){
            $errors = $validator->errors()->all();
            return e_msg(300001,$errors[0]);
        }

        $pass = User::password($data['password']);
        $info = User::where('username',$data['username'])
            ->where('password',$pass)
            ->first();
        if(!$info){
            $info = User::where('phone',$data['username'])
                ->where('password',$pass)
                ->first();
        }
        if($info){
            $auth = self::auth();
            if($token = $auth->login($info)){
                return s_msg('',[
                    'user_id' => $info->id,
                    'access_token' => $token,
                    'expires_in' => time()+86400,
                ]);
            }else{
                return e_msg(300001,'无法生成令牌');
            }
        }else{
            return e_msg(300003,'账号或密码不对');
        }

    }

    //设置手机号
    public static function phone($id,$phone){


        $user_id = self::where('id',$id)->value('username');
        if ( !$user_id ) {
            return e_msg(300015,'该用户不存在');
        }

        $update_arr = [
            'phone' => $phone,
        ];

        $rel = self::where('id',$id)->update($update_arr);

        if($rel){
            return s_msg('修改成功');

        }else{
            return e_msg(300016,'您没有作出修改');
        }
    }

    //重置密码
    public static function reset($id,$password){

//        $password = 'finger';

        $user_id = self::where('id',$id)->value('username');
        if ( !$user_id ) {
            return e_msg(300015,'该用户不存在');
        }

        $update_arr = [
            'password' => self::password($password),
        ];

        $rel = self::where('id',$id)->update($update_arr);

        if($rel){
            return s_msg('重置成功');

        }else{
            return e_msg(300016,'重置成功');
        }
    }

    //重设密码
    public function fog($data,$scene){

        $validateRule = [
            'phone'                          => 'required',
            'password'                            => 'required',
            'code'                           =>'required',
        ];
        $errorMsg = [
            'phone.required'                 => '手机号不能为空',
            'password.required'                   => '密码不能为空',
            'code.required'                   => '验证码不能为空',
        ];

        $validator = Validator::make($data, $validateRule, $errorMsg);
        if ($validator->fails()){
            $errors = $validator->errors()->all();
            return e_msg(300001,$errors[0]);
        }


        $key = $data['phone'].'_code_'.$scene;

        $code_info = Code::code($key,$data['code']);
        if($code_info['status'] === 'ERROR'){
            return $code_info;
        }

        $user_id = self::where('phone',$data['phone'])->value('id');
        if ( !$user_id ) {

            return e_msg(300015,'该手机号未注册');

        }


        $update_arr = [
            'password' => self::password($data['password']),
        ];

        $rel = self::where('id',$user_id)->update($update_arr);
        if($rel){
            return s_msg('');

        }else{
            return e_msg(300016,'您未做出修改');
        }
    }

    public function auth_user(){
        $auth = self::auth();
        if ($auth->check()) { # JWT同样可以使用Auth门面的check方法
            $data = $auth->user(); # JWT同样可以使用Auth门面的user方法
            return $data;
        } else {
            return e_msg(300007,'无法查询用户信息');
        }
    }

    public function logout(){
        $auth = self::auth();
        $auth->invalidate(true);
        return s_msg('');
    }
    public static function auth(){
        return app('auth')->guard('api');
    }

}
